<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class transaksi_keuangan extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_transaksi_keuangan');
	}
	
	function index(){
		$data['page_title']='Daftar Transaksi Keuangan Hari Ini';
		$data['page_icon']='icon-home';
		$data['page_content']='transaksi_keuangan_list';

		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		$data = $this->mdl_transaksi_keuangan->get_data();
		echo $this->mdl_transaksi_keuangan->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['aksi'] = 'add';

		$data['id'] = '';
        $data['kode_akun'] = '';
        $data['tgl_transaksi'] = date('d/m/Y');
        $data['tgl_posting'] = '';
        $data['referensi'] = '';
        $data['deskripsi'] = '';
        $data['debit'] = '';
        $data['kredit'] = '';
        $data['saldo'] = '';
		
		$this->load->view('transaksi_keuangan_form', $data);
	}

	function save(){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}

		# rules validasi form
		$this->form_validation->set_rules("kode_akun", 'Kode Akun', 'trim|required|xss_clean');
		$this->form_validation->set_rules("nominal", 'Nominal', 'trim|required|numeric|xss_clean');
		$this->form_validation->set_rules("deskripsi", 'Deskripsi', 'trim|required|xss_clean');

		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"] .= trim(form_error('kode_akun',' ',' '))==''?'':form_error('kode_akun',' ',' ').'<br>';
			$data["pesan_error"] .= trim(form_error('nominal',' ',' '))==''?'':form_error('nominal',' ',' ').'<br>';
			$data["pesan_error"] .= trim(form_error('deskripsi',' ',' '))==''?'':form_error('deskripsi',' ',' ').'<br>';
			
		}else{
			$result = $this->mdl_transaksi_keuangan->InsertOnDb($data);
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	}
}